/**********************************************************************************
 * Copyright (c) 2012 - 2012,  quick_tools, All right reserved
 * Title: com.adc.quicktools.file  WildcardUtil.java 
 * @author shen  shenqiao2012@163.com 
 *  2012-7-20 下午3:16:08 
 *********************************************************************************/


package com.adc.quicktools.file;

import com.adc.quicktools.StringUtil;



public class WildcardUtil {
	private final static String Matched_True="t";
	private final static String Matched_False="f";

	
	/**
	 * 
	 * Description: 根据传入的文件名（表达式）匹配本地路径下的文件
	 * author:  shen
	 * @param matchString
	 * @return boolean 
	 */
	@Deprecated
	public static boolean chkFileNameIsMatched(String matchString,String fileName) {
		boolean result=false;
		String tempFileName=StringUtil.compressionAsterisk(matchString);
		String[] strs=StringUtil.splitByChars(tempFileName, new Character[]{'*'});
		int[] questionCnt=StringUtil.countMultiQuestionMask(tempFileName);
		String s="";
		int i=0;
		for (int j = 0; j < strs.length; j++) {  // 根据通配符将目标字符串拆分，
//			System.out.println("ele:"+strs[j]);
			if (strs[j].contains("?")) {
				String questTemp=StringUtil.compressionQuestionMask(strs[j]);
				String[] strsTemp=StringUtil.splitByChars(questTemp, new Character[]{'?'});
				if (questionCnt[i]==strs[j].length()) {
					s=s.concat(Matched_True);
				}else {
					s=s.concat(Matched_False);
				}
				i++;
			}else {
				if (fileName.contains(strs[j])) {
					s=s.concat(Matched_True);
				}else {
					s=s.concat(Matched_False);
				}
			}
		}
//		System.out.println("s:"+s);
		result=chkMatchedOrNot(strs.length, s);
		return result;
	}

	/**
	 * 
	 * Description: 判断根据含有通配符的字符串匹配出的结果是否和预定目标一致性检查；
	 * author:  shen
	 * @param strArrayLength
	 * @param matchresult
	 * @return boolean
	 */
	@Deprecated
	private static boolean chkMatchedOrNot(int strArrayLength,String matchresult ) {
		boolean r=false;
		if (StringUtil.checkStringValue(matchresult)&&matchresult.length()==strArrayLength) {
			char[] temp=matchresult.toCharArray();
			for (int i = 0; i < temp.length; i++) {
				if (Matched_True.equals(temp[i]+"")) {
					r=true;
				}else {
					r=false;
					break;
				}
			}
		}
		return r;
	}
	
	/**
	 * 
	 * Description: 将文件名中含有的*、？处理成正则表达式对应的字符串，并返回正则表达式
	 * author:  shen
	 * @param wildcards 含有通配符*、？的字符串
	 * @return String
	 */
	public static String wildcardsToRegex(String wildcards) {
		String result="";
		if (StringUtil.checkStringValue(wildcards)) {
			if (wildcards.contains("*")||wildcards.contains("?")) {
				String target=StringUtil.compressionAsterisk(wildcards);
				target=target.replace("*", "(\\S|\\s)*");
				int[] cnts=StringUtil.countMultiQuestionMask(target);
				String tempString=StringUtil.compressionQuestionMask(target);
				for (int i = 0; i < cnts.length; i++) {
					tempString=tempString.replaceFirst("\\?", "\\\\S{"+cnts[i]+"}");
					System.out.println("[change to Regex]\t "+tempString+"\n");
				}
				System.out.println("[Finally Result] change["+wildcards+"] to ["+tempString+"]");
				result=tempString;
			}
		}
		return result;
	}
}
